package com.chima.service.impl;

import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.chima.dao.SonPostDao;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.SonPost;
import com.chima.service.SonPostService;

public class SonPostServiceImp implements SonPostService {

	private SonPostDao sonPostDao;
	
	public SonPostDao getSonPostDao() {
		return sonPostDao;
	}

	public void setSonPostDao(SonPostDao sonPostDao) {
		this.sonPostDao = sonPostDao;
	}

	
	public PageResponseBean findPost(PageRequestBean pageRequestBean) {

		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total =sonPostDao.findByTotalCount(pageRequestBean.getDept());
		// 获取分页查询条件
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Post.class);
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<SonPost> posts= sonPostDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(posts);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}

	public void savePost(SonPost sonPost) {
		sonPostDao.save(sonPost);
	}

	public void updatePost(SonPost sonPost) {
		sonPostDao.update(sonPost);
		
	}

	public void deletePost(int id) {
		
	   sonPostDao.delete(id);
		
	}


	public SonPost findPostByName(String name) {
		
		return sonPostDao.findPostByName(name);
	}

	
	public SonPost findPostById(int id) {
		
		return sonPostDao.findPostById(id);
	}


	public List<SonPost> findPostByPId(int id) {
	
		return sonPostDao.findsonPostBypId(id);
	}

}
